using System;
using System.ComponentModel.Composition;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using TrackMe.Apps.Views;
using TrackMe.Dto;

namespace TrackMe.Apps.VM
{
	[Export]
	[PartCreationPolicy(CreationPolicy.NonShared)]
	public class ProjectEditVM : BasicViewModel<IProjectEditView>
	{
		#pragma warning disable 0649
		[Import]
		private IDatabase database;
		#pragma warning restore 0649

		public ICommand SaveCmd { get; private set; }

		public IEditProject Model { get; set; }

		[ImportingConstructor]
		public ProjectEditVM(IProjectEditView view) : base(view)
		{
			SaveCmd = new DelegateCommand<string>(OnSave);
			RaisePropertyChanged(() => SaveCmd);
		}

		private void OnSave(string s)
		{
			if (Model.Name.Trim().Length == 0)
				throw new Exception("Name should not be empty");

			Model.Id = database.SaveProject(Model);
			ViewCore.Close();
		}

	}
}